<?php
require_once RGL_FS_WEB_ROOT."/app/_inc/class.rglPdf.php";

class rglDocGen {
	public $aDoc = array();
	public $aDocNewPag = array();
	public $aAnticipi = array();
	public $nR = -1;
	public $nC = 0;
	public $maxCol = 20;
	public $pdfIntestaz = "";
	public $bottomLeftText = "";
	public $aParRiga = array();
	public $pageWidth = 170;
	public $orientaPagina = 'P';
	
	public function __construct() {
		$this->aDoc = array();
		$this->aAnticipi = array();
		$this->nR = -1;
		$this->nC = 0;
		$this->pdfIntestaz = "";
		$this->aParRiga = array();
	}
	
	public function spaziatura($anticipo=0) {
		$this->nR = count($this->aDoc);
		$this->nC = -1;
		$this->aDoc[$this->nR] = $anticipo;
	}
	
	public function nuovaRiga($anticipo=0) {
		$this->nR = count($this->aDoc);
		$this->nC = -1;
		$this->aDoc[$this->nR] = array();
		$this->aAnticipi[$this->nR] = $anticipo;
	}
	
	public function setParRiga($par, $val) {
		$this->aParRiga[$this->nR][$par] = $val;
	}
	
	public function setCol($aCol) {
		if (count($this->aDoc)==0) return false;
		$this->nR = count($this->aDoc)-1;
		$this->nC = count($this->aDoc[$this->nR]);
		$this->aDoc[$this->nR][$this->nC] = $aCol;
	}
	
	public function nuovaRigaNP($anticipo=0) {
		$nR = count($this->aDocNewPag);
		$this->aDocNewPag[$nR] = array();
		$this->aAnticipi[$nR+10000] = 0;
	}
		
	public function setColNP($aCol) {
		if (count($this->aDocNewPag)==0) return false;
		$nR = count($this->aDocNewPag)-1;
		$nC = count($this->aDocNewPag[$nR]);
		$this->aDocNewPag[$nR][$nC] = $aCol;
	}

	public function forzaLabNP() {
		$this->nR = count($this->aDoc);
		foreach($this->aDocNewPag as $nRNP => $aRNP) {
			$this->aDoc[$this->nR] = $aRNP;
			$this->aAnticipi[$this->nR] = 0;
			$this->nR++;
		}
	}
	
	public function genHtml() {
		$sHtml = "<table class='tab1' border='0' cellpadding='3' cellspacing='1' width='100%' align='center'>";
		$sHtml .= getHtmlRigaRif($this->maxCol);
		foreach($this->aDoc as $rr => $aRX) {
			if (!is_array($aRX)) {
				$sHtml .= "\t<tr>\n";
				$sHtml .= "\t\t<td class='cmd1' colspan='40' style='$style' valign='top'>\n";
				$sHtml .= "\t\t\t&nbsp;\n";
				$sHtml .= "\t\t</td>\n";
				$sHtml .= "\t</tr>\n";
				continue;
			}
			$sHtml .= "\t<tr>\n";
			foreach($aRX as $cc => $aCX) {
				$classe = (isset($aCX['cla'])) ? "class='".$aCX['cla']."'" : "";
				$colspan = (isset($aCX['col']) and intval($aCX['col']) > 1) ?
					"colspan='".intval($aCX['col'])."'" : "";
				$style = "";
				if (isset($aCX['alg'])) {
					if ($aCX['alg']=="C") $style .= "text-align:center;";
					elseif ($aCX['alg']=="R") $style .= "text-align:right;";
				}
				if (isset($aCX['stf'])) {
					if ($aCX['stf']=="B" or $aCX['stf'] == "BI" or $aCX['stf'] == "IB") 
						$style .= "font-weight:bold;";
					if ($aCX['stf']=="I" or $aCX['stf'] == "BI" or $aCX['stf'] == "IB") 
						$style .= "font-style:italic;";
				}
				if (isset($aCX['stl'])) $style .= $aCX['stl'];	
				if (isset($aCX['htm']))	{
					$txt = $aCX['htm'];
				} else {
					$txt =  (isset($aCX['txt'])) ? $aCX['txt'] : "";
					$txt = str_replace("  "," &nbsp;",$txt);
					$txt = str_replace("\n","<br>",$txt);
					if (isset($aCX['hrf'])) {
						$txt = "<a $classe href='".$aCX['hrf']."' >$txt</a>";
					}
				}
				$sHtml .= "\t\t<td $classe $colspan style='$style' valign='top'>\n";
				$sHtml .= "\t\t\t$txt\n";
				$sHtml .= "\t\t</td>\n";
			}
			$sHtml .= "\t</tr>\n";
		}
		$sHtml .= "</table>\n";
		return $sHtml;
	}
	public function genPdf($aOpt=array()) {
		$this->orientaPagina = (isset($aOpt['orientaPagina']))?$aOpt['orientaPagina']:"P";
		$this->pageWidth = ($this->orientaPagina=='L')?257:170;
		$cr1 = 200; $cg1 = 200; $cb1 = 200;
		$pdf=new rglPdf();
		$pdf->topLeftText = $this->pdfIntestaz;
		//$pdf->topRightImage = EPS_FS_WEB_ROOT.'img/logo_e-stat.jpg';
		$pdf->bottomLeftText = $this->bottomLeftText;
		$pdf->Open();
		$this->AddPage($pdf);
		$pdf->SetTextColor(0,0,0);
		$pdf->SetFont('Arial','',7);
		$pdf->SetFillColor(255,255,255);
		foreach($this->aDoc as $rr => $aRX) {
			$this->scriviRiga($rr, $aRX, $pdf);
		}
		$strPdf = $pdf->Output('doc.pdf','S');
		return $strPdf;
	}

	private function scriviRiga($rr, $aRX, &$pdf) {
		if (!is_array($aRX)) {
			$pdf->Ln(5);
			return true;
		}
		if ($this->aAnticipi[$rr] < 0) {
			$this->AddPage($pdf);
		}
		$aWidths = array();
		$aStyles = array();
		$aAligns = array();
		$aBgColors = array();
		$aCols = array();
		$aBorders = array();
		$kCol = 0;
		foreach($aRX as $cc => $aCX) {	
			$fontSize = 7;			
			$aStyles[$kCol] = "";
			$aAligns[$kCol] = "";
			$aBgColors[$kCol] = null;
			$classe = (isset($aCX['cla'])) ? $aCX['cla'] : "txt1";
			$aBorders[$kCol] = (isset($aCX['brd'])) ? str_pad($aCX['brd'],4,"S") : "SSSS";
			switch ($classe) {
				case 'tit1':
					$aStyles[$kCol] = "B";
					$fontSize = 10;
					$aBgColors[$kCol] = array(200,200,200);
					break;
				case 'lab1':
					$aStyles[$kCol] = "I";
					$aBgColors[$kCol] = array(240,240,240);
					break;
				case 'txt2':
					$aBgColors[$kCol] = array(240,240,240);
					break;
				case 'txt1':
				default :
					break;
			}
			$colspan = (isset($aCX['col']) and intval($aCX['col']) > 1) ? intval($aCX['col']) : 1;
			$aWidths[$kCol] = $colspan*$this->pageWidth/$this->maxCol;
			$style = "";
			if (isset($aCX['alg'])) {
				if ($aCX['alg']=="C") $aAligns[$kCol] = "C";
				elseif ($aCX['alg']=="R") $aAligns[$kCol] = "R";
			}
			$aStyles[$kCol] = (isset($aCX['stf'])) ? $aCX['stf'] : "";
			$aCols[$kCol] = (isset($aCX['txt'])) ? $aCX['txt'] : "";
			$kCol++;
		}
		if (isset($this->aParRiga[$rr]['pdf_fontSize'])) {
			$fontSize = $this->aParRiga[$rr]['pdf_fontSize'];
		}
		if ($pdf->FontSizePt != $fontSize) {
			$pdf->SetFont('Arial','',$fontSize);
		}
		//Calculate the height of the row
		$hRiga = $pdf->FontSizePt / 2;
		$nb=0;
		for($i=0;$i<count($aCols);$i++) {
			$nb=max($nb,$pdf->NbLines($aWidths[$i],$aCols[$i]));
		}
		$anticipo = ($this->aAnticipi[$rr]>0)?$this->aAnticipi[$rr]:0;
		$h=$hRiga*($nb+$anticipo);
		//Issue a page break first if needed
		//dbg_("Anticipo=".$this->aAnticipi[$rr].", nb=$nb; hRiga=$hRiga, h=$h, GetY=".$pdf->GetY());
		if($rr < 10000 && ($pdf->GetY()+$h) > $pdf->PageBreakTrigger) {
			$this->AddPage($pdf);
			if (isset($this->aParRiga[$rr]['labNP'])) {
				foreach($this->aDocNewPag as $rr => $aRX) {
					$this->scriviRiga($rr+10000, $aRX, $pdf);
				}
			}
		}		
		if ($pdf->FontSizePt != $fontSize) {
			$pdf->SetFont('Arial','',$fontSize);
		}
		$pdf->SetWidths($aWidths);
		$pdf->SetStyles($aStyles);
		$pdf->SetAligns($aAligns);
		$pdf->SetBgColors($aBgColors);
		$pdf->SetBorders($aBorders);
		$pdf->Row($aCols);
		return true;
	}
	
	private function AddPage(&$pdf) {
		$pdf->AddPage($this->orientaPagina);
	}
} // fine classe rglDocGen